﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Happy.Web.Mvc
{
    /// <summary>
    /// 提供控制器级别的默认 ModelBinder。
    /// </summary>
    public sealed class HappyModelBinder : IModelBinder
    {
        private static readonly DefaultModelBinder _defaultModelBinder = new DefaultModelBinder();

        /// <inheritdoc />
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var supportDefaultModelBinder = controllerContext.Controller as ISupportDefaultModelBinder;
            if (supportDefaultModelBinder != null && supportDefaultModelBinder.DefaultModelBinder != null)
            {
                return supportDefaultModelBinder.DefaultModelBinder.BindModel(controllerContext, bindingContext);
            }

            return _defaultModelBinder.BindModel(controllerContext, bindingContext);
        }
    }
}
